package com.msf.swingdemo.container;

import com.formdev.flatlaf.FlatLightLaf;
import com.msf.swingdemo.utils.IconUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;

@SpringBootApplication
public class GlassExample extends JFrame {
    JPanel glass;
    JProgressBar waiter;
    Timer timer;

    public GlassExample() {
        super("GlassPane Demo");
        try {
            UIManager.setLookAndFeel(new FlatLightLaf());

            setSize(500, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            initFrame();

            setLocationRelativeTo(null);
            setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void initFrame() {
        // Set up the glass pane with a little message and a progress bar.
        JPanel controlPanel = new JPanel(new GridLayout(2, 1));
        controlPanel.setOpaque(false);
        controlPanel.add(new JLabel("Please wait..."));
        waiter = new JProgressBar(0, 100);
        controlPanel.add(waiter);

        glass = new JPanel(new GridLayout(0, 1));
        glass.setOpaque(false);
        glass.add(new JLabel());    // padding...
        glass.add(new JLabel());
        glass.add(controlPanel);
        glass.add(new JLabel());
        glass.add(new JLabel());
        glass.addMouseListener(new MouseAdapter() {});
        glass.addMouseMotionListener(new MouseMotionAdapter() {});
        setGlassPane(glass);

        // Now set up a few buttons and images for the main application.
        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(Color.white);
        JButton redButton = new JButton("Red");
        JButton greenButton = new JButton("Green");
        JButton blueButton = new JButton("Blue");
        mainPanel.add(redButton);
        mainPanel.add(greenButton);
        mainPanel.add(blueButton);
        mainPanel.add(new JLabel(IconUtil.Oreilly_Image));

        // Attach the pop-up debugger to the main app buttons so you
        // see the effect of making a glass pane visible.
        PopupDebugger pd = new PopupDebugger(this);
        redButton.addActionListener(pd);
        greenButton.addActionListener(pd);
        blueButton.addActionListener(pd);

        // And last but not least, our button to launch the glass pane
        JButton startB = new JButton("Start the big operation");
        startB.addActionListener(e -> {
            glass.setVisible(true);
            startTimer();
        });

        Container contentPane = getContentPane();
        contentPane.add(mainPanel, BorderLayout.CENTER);
        contentPane.add(startB, BorderLayout.SOUTH);
    }

    // A quick method to start up a 10-second timer and update the progress bar
    public void startTimer() {
        if (timer == null) {
            timer = new Timer(1000, new ActionListener() {
                int progress = 0;
                @Override
                public void actionPerformed(ActionEvent e) {
                    progress += 10;
                    waiter.setValue(progress);

                    // Once we hit 100%, remove the glass pane and reset the progress bar
                    // stuff.
                    if (progress >= 100) {
                        progress = 0;
                        timer.stop();
                        glass.setVisible(false);
                        waiter.setValue(0);
                    }
                }
            });
        }
        if (timer.isRunning()) {
            timer.stop();
        }
        timer.start();
    }

    // A graphical debugger that pops up whenever a button is pressed
    public class PopupDebugger implements ActionListener {
        private JFrame parent;
        public PopupDebugger(JFrame f) {
            parent = f;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(parent, e.getActionCommand());
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(GlassExample.class, args);
    }
}
